this.Food=this.Food||{};

/**
 * dom
 * domc操作类
 */
var $=function(arg){
	return new Food.dom.init(arg);
};
(function(fd){
	fd.dom={
		init:function(arg){
			this[0]=typeof arg=='string'?document.getElementById(arg):arg;
			return this;
		},
		html:function(arg){
			return arg===undefined?this[0].innerHTML:(this[0].innerHTML=arg,this);
		},
		css:function(name,value){
			this[0].style[name]=value;
			return this
		},
		cls:function(value){
			this[0].className=value;
			return this;
		},
		show:function(){
			this.css('display','');
			return this;
		},
		hide:function(){
			this.css('display','none');
			return this;
		},
		append:function(elem){
			this[0].appendChild(elem);
			return this;
		},
		evt:function(type,hdl){
			var elem=this[0];
			elem[elem.addEventListener?'addEventListener':(type='on'+type,'window.attachEvent')](type,function(){
				var evt=window.event||evt,target=evt.srcElement||evt.target;
				hdl(evt,target,elem);
			},false)
			
			return this;
		},
		transition:function(duration,fn,prop){
			var adapter=['MozT','WebkitT','t'];
			for(var i=0;i<adapter.length;i++){
				this.css(adapter[i]+'ransition-property',prop||'all');
				this.css(adapter[i]+'ransition-duration',(duration||'0.5')+'s');
				this.css(adapter[i]+'ransition-timing-function',fn||'ease-out');
				this.css(adapter[i]+'ransition-delay',0);
			}
			return this;
		},
		totate:function(deg){
			var adapter=['MozT','WebkitT','t'];
			for(var i=0;i<adapter.length;i++){
				this.css(adapter[i]+'ransform','rotate('+(deg||60)+'deg)');
			}
			return this;
		},
		template:function(tmpl,json,list){
			var html=$(tmpl).html(),temp=html;
			json=json.length?json:[json];

			var arr=[];
			for(var i=0;i<json.length;i++){
				temp=html;
				for(var p in json[i]){
					temp=temp.replace(new RegExp('<%='+p+'%>','g'),json[i][p]);
				}
				arr.push(temp);
			}			
			
			this.html(arr.join(''));
			return this;
		},
		desk:function(single/*单个桌面长度*/,target/*指定桌面位置*/,min,max){
			if(target<min||target>max){
				return;
			}
			var distance=(target-1)*single;
			this.transition(null,'ease-in-out').css('left',-distance+'px');
		}
	};
	fd.dom.init.prototype=fd.dom;
})(this.Food);


/**
 * http
 * 数据交互类
 */
(function(fd){
	var fac=function(){
		return new XMLHttpRequest;
	},fn=function(){};
	
	fd.http={
		get:function(url,success,error){		
			this.request({
				url:url,
				success:success,
				error:error
			});
		},
		post:function(url,params,success,error){
			this.request({
				method:'POST',
				url:url,
				params:params,
				success:success,
				error:error
			});

		},
		request:function(options){
			var xhr=fac(),data={
				method:'GET',
				params:null,
				success:fn,
				error:fn
			};
			for(var i in options)
			{
				options[i]&&(data[i]=options[i]);
			}
			xhr.onreadystatechange=function(){
				xhr.readyState==4&&(xhr.status==200&&(data.success(eval('('+xhr.responseText+')'))),data.error(xhr.status));
			};
			xhr.open(data.method,data.url,true);
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xhr.send(data.params);
		}
	};
})(this.Food);

